//
//  LoginViewController.m
//  MyzcApp
//
//  Created by myzc on 16/6/23.
//  Copyright © 2016年 myzc. All rights reserved.
//

#import "LoginViewController.h"
#import "MyzcViewController.h"
#import "AFHTTPSessionManager.h"
#import "UIKit+AFNetworking.h"
@interface LoginViewController ()<UITextFieldDelegate>
@property (nonatomic, assign) NSInteger  Countdown;//倒计时
@property (nonatomic, assign) NSTimer *timer;
@property (nonatomic, strong)NSTimer *remainingSeconds;
@property (nonatomic, strong) UIButton *sendCodeBt;
@property (nonatomic, strong) UITextField *phoneText;
@property (nonatomic, strong)UITableView *tableview;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _Countdown = 60;
    
    [self CreateViewSubviewButton];
    

}

- (void)CreateViewSubviewButton{
self.navigationController.navigationBar.translucent = NO;
self.navigationItem.title = @"登入窗口";

self.view.backgroundColor = [UIColor lightGrayColor];

UILabel *phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, self.view.frame.size.height / 6, 40, 40)];
phoneLabel.text = @"手机";
phoneLabel.backgroundColor = [UIColor clearColor];
phoneLabel.font = [UIFont fontWithName:@"Arial" size:18]; //指定字体的方法
phoneLabel.highlighted = YES;
phoneLabel.adjustsFontSizeToFitWidth = YES;


UILabel *codeLabel = [[UILabel alloc] initWithFrame:CGRectMake(phoneLabel.frame.origin.x, phoneLabel.frame.origin.y + phoneLabel.frame.size.height * 2, phoneLabel.frame.size.width + 15, phoneLabel.frame.size.height)];
codeLabel.text = @"验证码";
codeLabel.backgroundColor = [UIColor clearColor];
codeLabel.font = [UIFont fontWithName:@"Arial" size:18]; //指定字体的方法
codeLabel.highlighted = YES;
codeLabel.adjustsFontSizeToFitWidth = YES;


_phoneText = [[UITextField alloc] initWithFrame:CGRectMake(phoneLabel.frame.origin.x * 2 + phoneLabel.frame.size.width, phoneLabel.frame.origin.y, self.view.frame.size.width - phoneLabel.frame.origin.x * 4, phoneLabel.frame.size.height)];
_phoneText.borderStyle = UITextBorderStyleRoundedRect;
//    phoneText.backgroundColor = [UIColor clearColor];
_phoneText.placeholder = @"130000000000";
//是否纠错
_phoneText.autocorrectionType = UITextAutocorrectionTypeYes;
//输入框中是否有个叉号，在什么时候显示，用于一次性删除输入框中的内容
_phoneText.clearButtonMode = UITextFieldViewModeAlways;
//设置输入框内容的字体样式和大小
_phoneText.font = [UIFont fontWithName:@"Arial" size:18.0f];
//设置键盘的样式
_phoneText.keyboardType = UIKeyboardTypeNumberPad;


UITextField *codeText = [[UITextField alloc] initWithFrame:CGRectMake(_phoneText.frame.origin.x, codeLabel.frame.origin.y, codeLabel.frame.size.width, codeLabel.frame.size.height)];
codeText.borderStyle = UITextBorderStyleRoundedRect;
//    codeText.backgroundColor = [UIColor clearColor];
codeText.placeholder = @"****";
//是否纠错
codeText.autocorrectionType = UITextAutocorrectionTypeYes;
//输入框中是否有个叉号，在什么时候显示，用于一次性删除输入框中的内容
codeText.clearButtonMode = UITextFieldViewModeAlways;
//设置输入框内容的字体样式和大小
codeText.font = [UIFont fontWithName:@"Arial" size:18.0f];
//设置键盘的样式
codeText.keyboardType = UIKeyboardTypeNumberPad;


//    UIButton *sendCodeBt = [UIButton buttonWithType:UIButtonTypeRoundedRect];
_sendCodeBt  = [[UIButton alloc] initWithFrame:CGRectMake(codeText.frame.origin.x + codeText.frame.size.width + codeLabel.frame.size.width, codeText.frame.origin.y, self.view.frame.size.width -(codeText.frame.origin.x + codeText.frame.size.width + codeLabel.frame.size.width) -phoneLabel.frame.size.width  , codeText.frame.size.height)];

_sendCodeBt.backgroundColor = [UIColor grayColor];
[_sendCodeBt setTitle:@"获取验证码" forState:UIControlStateNormal];


//[_sendCodeBt setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    
 
[_sendCodeBt setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    _sendCodeBt.selected = YES;
_sendCodeBt.titleLabel.font = [UIFont systemFontOfSize: 13.0];




UIButton *loginBt =[[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 3, _phoneText.frame.origin.y * 3,_sendCodeBt.frame.size.width, _sendCodeBt.frame.size.height)];

loginBt.backgroundColor = [UIColor grayColor];
[loginBt setTitle:@"快速登入" forState:UIControlStateNormal];
loginBt.titleLabel.font = [UIFont systemFontOfSize: 17.0];



[_sendCodeBt addTarget:self action:@selector(actionSendCodeBt:) forControlEvents:UIControlEventTouchUpInside];

[loginBt addTarget:self action:@selector(actionLoginBt:) forControlEvents:UIControlEventTouchUpInside];


codeText.delegate = self;
_phoneText.delegate = self;
[self.view addSubview:phoneLabel];
[self.view addSubview:codeLabel];
[self.view addSubview:_phoneText];
[self.view addSubview:codeText];
[self.view addSubview:_sendCodeBt];
[self.view addSubview:loginBt];
}

- (void)actionSendCodeBt:(UIButton *)bt
{
    
    [_sendCodeBt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _sendCodeBt.selected = YES;
    //启动timer
    //初始化timer
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];


    //设置sendCodeBt不可点击
    [_sendCodeBt setUserInteractionEnabled:NO];
    
    //post请求获取验证码数据
    // 创建AFHTTPSessionManager对象
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    //上传json格式
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    
    //声明获取到的数据格式 AFN会json解析返回的数据   有时接口返回的数据不合格会报3840错误，大致是AFN无法解析返回来的数据
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    // 创建请求类
    //POST
    
    [manager  POST: [NSString stringWithFormat:@"http://114.55.115.175:8080/myzc98Server/smsCodes?act=login"] parameters:@{@"mobile":[NSString stringWithFormat:@"%@",self.phoneText.text]} progress:^(NSProgress * _Nonnull downloadProgress) {
        // 这里可以获取到目前数据请求的进度
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 请求成功
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        //             NSLog(@"%@", dict);
        
        NSMutableDictionary  *dic = [dict objectForKey:@"mobile"];
        
    
        [self.tableview reloadData];
        
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // 请求失败
        
        NSLog(@"%@", [error  localizedDescription]);
        
    }];
    
    

}
- (void)timerAction:(NSTimer *)tm
{
    _Countdown -- ;
   
    if (_Countdown == 0) {
        //取消定时器 timer 停止
        [_timer invalidate];
        _timer = nil;
        
   
        [_sendCodeBt setTitle:@"重获验证码" forState:UIControlStateNormal];
        [_sendCodeBt setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        _sendCodeBt.selected = YES;
     

        _Countdown = 60;
        [_sendCodeBt setUserInteractionEnabled:YES];
        
        return;
    }
    [_sendCodeBt setTitle:[NSString stringWithFormat:@"%lds后重发验证码", _Countdown] forState:UIControlStateNormal];


}
- (void)actionLoginBt:(UIButton *)bt
{
    MyzcViewController *myzcVC = [[MyzcViewController alloc] init];
    [self.navigationController pushViewController:myzcVC animated:YES];
}
//点击空白处 键盘回收
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    [self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
